package com.wangcang.widgetlibrary.bubble;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.wangcang.widgetlibrary.R;
import com.wangcang.widgetlibrary.adapter.BubblePopAdapter;
import com.wangcang.widgetlibrary.popupwindow.EasyPopupWindow;

import java.util.List;

/**
 * 泡泡弹框工具类
 * Created by ShineF on 2017/6/28 0028.
 */

public class PopUpUtils {

    /**
     * 箭头朝上
     *
     * @param view    相对的控件
     * @param context 上下文
     */
    public static void showBubbleUp(View view, Context context, String text) {
        View bubbleView = View.inflate(context, R.layout.view_bubble_pop_top, null);
        TextView tvContent = (TextView) bubbleView.findViewById(R.id.tv_content);
        tvContent.setText(text);
        EasyPopupWindow easyPopupWindow = new EasyPopupWindow.Builder(context)
                .setView(bubbleView)
                .setSize(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                .setOutsideTouchHide(true)
                .create();
        easyPopupWindow.showAsDropDown(view);
    }

    /**
     * 箭头朝上2
     *
     * @param view    相对的控件
     * @param context 上下文
     */
    public static void showBubbleUp2(View view, Context context, int position, final OnChoiceListener onChoiceListener) {
        View bubbleView = View.inflate(context, R.layout.view_bubble_pop_top2, null);
        LinearLayout LLChoice1 = (LinearLayout) bubbleView.findViewById(R.id.ll_choice1);
        LinearLayout LLChoice2 = (LinearLayout) bubbleView.findViewById(R.id.ll_choice2);
        final ImageView imageView = (ImageView) bubbleView.findViewById(R.id.choice1);
        final ImageView imageView2 = (ImageView) bubbleView.findViewById(R.id.choice2);
        choice(position, imageView, imageView2);
        View.OnClickListener onClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.ll_choice1:
                        choice(1, imageView, imageView2);
                        onChoiceListener.onChoice(1);
                        break;
                    case R.id.ll_choice2:
                        choice(2, imageView, imageView2);
                        onChoiceListener.onChoice(2);
                        break;
                }
            }
        };
        LLChoice1.setOnClickListener(onClickListener);
        LLChoice2.setOnClickListener(onClickListener);
        EasyPopupWindow easyPopupWindow = new EasyPopupWindow.Builder(context)
                .setView(bubbleView)
                .setSize(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                .setOutsideTouchHide(true)
                .create();
//        easyPopupWindow.showAsDropDown(view);
        easyPopupWindow.showAtLocation(view, Gravity.RIGHT | Gravity.TOP, 0, 0);
    }

    private static void choice(int position, View v1, View v2) {
        switch (position) {
            case 1:
                v1.setVisibility(View.VISIBLE);
                v2.setVisibility(View.INVISIBLE);
                break;
            case 2:
                v1.setVisibility(View.INVISIBLE);
                v2.setVisibility(View.VISIBLE);
                break;
            default:
                v1.setVisibility(View.INVISIBLE);
                v2.setVisibility(View.INVISIBLE);
                break;
        }
    }

    public interface OnChoiceListener {
        void onChoice(int position);
    }

    /**
     * 箭头朝左
     *
     * @param view    相对的控件
     * @param context 上下文
     */
    public static void showBubbleLeft(View view, Context context, List<String> list, BubblePopAdapter.OnPopListClickListener listener) {
        View bubbleView = View.inflate(context, R.layout.view_bubble_pop_left, null);
        ListView mLvList = (ListView) bubbleView.findViewById(R.id.lv_list);
        BubblePopAdapter adapter = new BubblePopAdapter(context, list);
        mLvList.setAdapter(adapter);
        adapter.setOnPopListClickListener(listener);
        EasyPopupWindow easyPopupWindow = new EasyPopupWindow.Builder(context)
                .setView(bubbleView)
                .setSize(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                .setOutsideTouchHide(true)
                .create();
        int[] location = new int[2];
        view.getLocationInWindow(location);
        easyPopupWindow.showAtLocation(view, Gravity.LEFT, location[0] + view.getWidth(), 0);
    }

    /**
     * 箭头朝右
     *
     * @param view    相对的控件
     * @param context 上下文
     */
    public static void showBubbleRight(View view, Context context, String text) {
        View bubbleView = View.inflate(context, R.layout.view_bubble_pop_right, null);
        TextView tvContent = (TextView) bubbleView.findViewById(R.id.tv_content);
        tvContent.setText(text);
        EasyPopupWindow easyPopupWindow = new EasyPopupWindow.Builder(context)
                .setView(bubbleView)
                .setSize(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                .setOutsideTouchHide(true)
                .create();
        easyPopupWindow.showAtLocation(view, Gravity.RIGHT, view.getWidth(), 0);

    }

    /**
     * 箭头朝下
     *
     * @param view    相对的控件
     * @param context 上下文
     * @param text    内容
     */
    public static void showBubbleBottom(View view, Context context, String text) {
        View bubbleView = View.inflate(context, R.layout.view_bubble_pop_bottom, null);
        TextView tvContent = (TextView) bubbleView.findViewById(R.id.tv_content);
        tvContent.setText(text);
        EasyPopupWindow easyPopupWindow = new EasyPopupWindow.Builder(context)
                .setView(bubbleView)
                .setSize(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                .setOutsideTouchHide(true)
                .create();
        int[] location = new int[2];
        view.getLocationInWindow(location);
        easyPopupWindow.showAtLocation(view, Gravity.BOTTOM, 0, view.getHeight());
    }

    /**
     * 自定义箭头朝下
     *
     * @param view    相对的控件
     * @param context 上下文
     * @param gravity 针对父布局的位置
     * @param x       距离X轴距离
     * @param y       距离Y轴距离
     */
    public static void showBubbleBottom(View view, Context context, int gravity, int x, int y, String text) {
        View bubbleView = View.inflate(context, R.layout.view_bubble_pop_bottom, null);
        TextView tvContent = (TextView) bubbleView.findViewById(R.id.tv_content);
        tvContent.setText(text);
        EasyPopupWindow easyPopupWindow = new EasyPopupWindow.Builder(context)
                .setView(bubbleView)
                .setSize(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                .setOutsideTouchHide(true)
                .create();
        int[] location = new int[2];
        view.getLocationInWindow(location);
        easyPopupWindow.showAtLocation(view, gravity, x, y);
    }

    public static EasyPopupWindow showPopUpBottom(int layout, Context context, View parent, boolean touchOutHide, EasyPopupWindow.ChildClickListener listener) {
        EasyPopupWindow easyPopupWindow = new EasyPopupWindow.Builder(context)
                .setView(layout) //设置popupwindow view 里面可以传view, layout
                .setSize(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) //设置宽高
                .setBackgroundAlpha(0.7f) //设置背景透明度
                .setAnim(R.style.anim_menu_bottom_bar) //设置动画
                .setOutsideTouchHide(touchOutHide) //点击外面是否关闭
                .setOnChildClickListener(listener) //布局中的事件回调
                .create(); //创建

        easyPopupWindow.showAtLocation(parent, Gravity.BOTTOM, 0, 0);

        return easyPopupWindow;
    }

    public static EasyPopupWindow showPopUpTop(int layout, Context context, View parent, boolean touchOutHide, EasyPopupWindow.ChildClickListener listener) {
        EasyPopupWindow easyPopupWindow = new EasyPopupWindow.Builder(context)
                .setView(layout)
                .setSize(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
//                .setBackgroundAlpha(0.7f)
                .setAnim(R.style.anim_menu_top_bar)
                .setOutsideTouchHide(touchOutHide)
                .setOnChildClickListener(listener)
                .create();

        easyPopupWindow.showAsDropDown(parent, 0, 0);

        return easyPopupWindow;
    }
}
